<template>
	<div class="app-container">
		
        <el-table :data="tableData" style="width: 100%">
            <el-table-column prop="name" label="名字"></el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                <div class="operation-wrapper">
                    <el-button type="primary" size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDelete(scope.$index)">删除</el-button>
                </div>
                </template>
            </el-table-column>
        </el-table>

		<el-dialog :fullscreen="true" :visible.sync="dialogVisible" title="编辑" :close-on-click-modal="false">
			<el-form :model="editForm" :rules="editFormRules" ref="editForm" label-width="80px">
				<el-form-item label="名字" prop="name">
					<el-input v-model="editForm.name"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="dialogVisible = false">取消</el-button>
				<el-button type="primary" @click="saveEdit">保存</el-button>
			</div>
		</el-dialog>

		<el-dialog :fullscreen="true" :visible.sync="addDialogVisible" title="添加新Gel卡" :close-on-click-modal="false">
			<el-form :model="addForm" :rules="addFormRules" ref="addForm" label-width="80px">
				<el-form-item label="名字" prop="name">
					<el-input v-model="addForm.name"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="addDialogVisible = false">取消</el-button>
				<el-button type="primary" @click="saveAdd">添加</el-button>
			</div>
		</el-dialog>
		<el-button style="margin-top: 20px;" type="primary" @click="addDialogVisible = true">添加新Gel卡</el-button>

	</div>
</template>

<script>
import Api from '../../api/api'
export default {
	data() {
		return {
			tableData: [],
			dialogVisible: false,
			addDialogVisible: false,
			editFormIndex: 0,
			tempForm: {
				name: '',
			},
			editForm: {
				name: '',
			},
			addForm: {
				name: '',
			},
			editFormRules: {
				name: [{
					required: true,
					message: '请输入名字',
					trigger: 'blur'
				}]
			},
			addFormRules: {
				name: [{
					required: true,
					message: '请输入名字',
					trigger: 'blur'
				}]
			}
		};
	},
	methods: {
		onLoad() {
			Api.getGelCards().then(response => {
				this.tableData = response.data.data
				console.log(response.data)
			})
		},
		handleEdit(index, row) {
			this.editForm = Object.assign({}, row);
			this.editFormIndex = index;
			this.dialogVisible = true;
		},
		saveEdit() {
			this.$refs.editForm.validate(valid => {
				if (valid) {
					console.log(this.editForm)
					console.log(this.editFormIndex)
					console.log(this.tableData[this.editFormIndex])
					let date = this.tableData[this.editFormIndex]
					Api.updaGelCards({ id: date._id, date: this.editForm }).then(response => {
						console.log(response.data)
						if (response.status == 200) {
							Object.assign(this.tableData[this.editFormIndex], this.editForm);
							this.dialogVisible = false;
						}
						this.$message.success(response.data.message);
					})
				}
			});
		},
		handleDelete(index) {
			this.$confirm('确认删除此Gel卡？')
				.then(_ => {
					let date = this.tableData[index]
					console.log(date)
					Api.delGelCards({ id: date._id }).then(response => {
						console.log(response.data)
						if (response.status == 200) {
							this.tableData.splice(index, 1);

						}
						this.$message.success(response.data.message);
					})

				})
				.catch(_ => { });
		},
		saveAdd() {
			this.$refs.addForm.validate(valid => {
				if (valid) {
					Api.createGelCard(this.addForm).then(response => {
						console.log(response.data)
						if (response.status == 200) {
							this.tableData.push({ ...response.data.data});
							this.addForm = {...response.data.data}
							delete this.addForm._id
							this.addDialogVisible = false;

						}
						this.$message.success(response.data.message);

					})

				}
			});
		}
	}
};
</script>
<style>
.app-container {
	display: flex;
	flex-direction: column;
	height: 100%;
}
.operation-wrapper {
  display: flex;
  justify-content: flex-end;
}
</style>
